#include "server.h"

void *threadFunc(void *arg) {
    char buf[1024] = {0};
    char cmd[128] = {0};
    char path1[128] = {0};
    char path2[128] = {0};
    threadPool_t *pThreadPool = (threadPool_t *) arg;

    /* 建立 MYSQL 连接 */
    pthread_mutex_lock(&pThreadPool->mutex);
    MYSQL *mysql = mysql_init(NULL);
    MYSQL *ret = mysql_real_connect(mysql, "localhost", "root", "123", "myCloud", 0, NULL, 0);
    if (!ret) {
        fprintf(stderr,"mysql_real_connect: %s\n", mysql_error(mysql));
        return NULL;
    }
    pthread_mutex_unlock(&pThreadPool->mutex);
    printf("mysql connected\n");


    while (1) {
        
        /* 队列中有任务就工作, 没任务就等待 */
        pthread_mutex_lock(&pThreadPool->mutex);
        while (!pThreadPool->taskQueue.queueSize) {
            pthread_cond_wait(&pThreadPool->cond, &pThreadPool->mutex);
        }
        printf("Worker get a job\n");
        int netFd = pThreadPool->taskQueue.front->netFd;
        deQueue(&pThreadPool->taskQueue);
        pthread_mutex_unlock(&pThreadPool->mutex);
        
        /* 接收消息 */
        int recvRet = recvMessageFromClient(netFd, buf, sizeof(buf));
        if (!recvRet) {
            close(netFd);
            printf("One Client Disconnected\n");
            printf("Finished\n\n");
            continue;
        }
        printf("%s", buf);
        LOG_WRITE(buf);

        /* 解析命令 */
        memset(cmd,0,sizeof(cmd));
        memset(path1,0,sizeof(path1));
        memset(path2,0,sizeof(path2));
        cutCmd(buf, cmd, path1, path2);

        if (strcmp(cmd, "cd") == 0) {
            /* strcpy(pThreadPool->userBuffer[netFd], "liu"); */
            int cdRet = changeDir(mysql, pThreadPool->userBuffer[netFd], pThreadPool->pathBuffer[netFd], path1);
            if (cdRet == -1) {
                sendMessageToClient(netFd, "cd: directory does not exist", 28);
            } else {
                sendMessageToClient(netFd, pThreadPool->pathBuffer[netFd], strlen(pThreadPool->pathBuffer[netFd]));
            }

        } else if (strcmp(cmd, "ls") == 0) {
            char lsTxt[512] = {0};
            /* strcpy(pThreadPool->userBuffer[netFd], "liu"); */

            listSearch(mysql, pThreadPool->userBuffer[netFd], pThreadPool->pathBuffer[netFd], lsTxt);
            strcat(lsTxt, " ");
            sendMessageToClient(netFd, lsTxt, strlen(lsTxt));

        } else if (strcmp(cmd, "pwd") == 0) {
           
            printf("%s\n", pThreadPool->pathBuffer[netFd]);            
            sendMessageToClient(netFd, pThreadPool->pathBuffer[netFd], strlen(pThreadPool->pathBuffer[netFd]));

        } else if (strcmp(cmd, "remove") == 0) {
            
            int removeRet = removeFile(mysql, pThreadPool->userBuffer[netFd], pThreadPool->pathBuffer[netFd], path1);
            if (removeRet == 0) {
                sendMessageToClient(netFd, "remove failed", 13);
            } else {
                sendMessageToClient(netFd, "remove finished", 15);
            }

        } else if (strcmp(cmd, "mkdir") == 0) {
            
            int mkdirRet = mkDir(mysql, pThreadPool->userBuffer[netFd], pThreadPool->pathBuffer[netFd], path1);
            if (mkdirRet == 0) {
                sendMessageToClient(netFd, "mkdir failed", 12);
            } else {
                sendMessageToClient(netFd, "mkdir finished", 14);
            }

        } else if (strcmp(cmd, "rmdir") == 0) {
            
            int rmdirRet = rmDir(mysql, pThreadPool->userBuffer[netFd], pThreadPool->pathBuffer[netFd], path1);
            if (rmdirRet == 0) {
                sendMessageToClient(netFd, "rmdir failed", 12);
            } else {
                sendMessageToClient(netFd, "rmdir finished", 14);
            }

        } else if (strcmp(cmd, "puts") == 0) {
            
            char left[256] = {0};
            char right[256] = {0};
            printf("cutTail begin\n");
            cutTail(path2, left, right);
            printf("%s, %s\n", left, right);

            char currPath[256] = {0};
            strcpy(currPath, pThreadPool->pathBuffer[netFd]);
            int ret = changePath(mysql, pThreadPool->userBuffer[netFd], currPath, left);
            if (ret == -1) {
                send(netFd, "0", 1, 0);
                printf("Filished\n\n");
                epollAdd(pThreadPool->epollFd, netFd);
                continue;
            }
            
            char md5[128] = {0};
            ssize_t r = recvMessageFromClient(netFd, md5, sizeof(md5));
            if (!r) {
                close(netFd);
                printf("One Client Disconnected\n");
                printf("Finished\n\n");
                continue;
            }
            int pre_id = 0;
            getId(pThreadPool->userBuffer[netFd], currPath, &pre_id, mysql);
            printf("%d\n", pre_id);
            if (strcmp(currPath, "/")) {
                strcat(currPath, "/");
            }
            strcat(currPath, right);
            puts(currPath);
            char size[64] = {0};
            if (searchMd5(md5, mysql, size)) {
                printf("md5 exist\n");
                int r = delToMk(mysql, pThreadPool->userBuffer[netFd], pThreadPool->pathBuffer[netFd], path2);
                if (r == 0) {
                    insertFile(right, currPath, atoi(size), md5, pThreadPool->userBuffer[netFd], pre_id, mysql); 
                }
                send(netFd, "1", 1, 0);
            } else {
                send(netFd, "0", 1, 0);
                recvFileFromClient(netFd, mysql, pThreadPool , pre_id, currPath);
                sendMessageToClient(netFd, "puts finished", 13);
            }

        } else if (strcmp(cmd, "gets") == 0) {
            /* strcpy(pThreadPool->userBuffer[netFd], "liu"); */
            
            char currPath[256] = {0};
            strcpy(currPath, pThreadPool->pathBuffer[netFd]);
            printf("%s\n", currPath);
            int ret = changePath(mysql, pThreadPool->userBuffer[netFd], currPath, path1);
            if (ret == -1) {
                send(netFd, "0", 1, 0);
                printf("Filished\n\n");
                epollAdd(pThreadPool->epollFd, netFd);
                continue;
            }
            char md5Path[256] = {0};
            searchFile(currPath, mysql, pThreadPool->userBuffer[netFd], md5Path, pThreadPool->home);
            send(netFd, "1", 1, 0);
            sendMessageToClient(netFd, "geting......", 12);
            sendFileToClient(netFd, md5Path, path2);
        }
        
        /* 一个客户端一条任务执行完才能重新开始监听, 避免多线程冲突 */
        printf("Filished\n\n");
        epollAdd(pThreadPool->epollFd, netFd);
    }
}
